import { reqLogin } from '@/api/common'
import { IloginForm } from '@/api/common/types'
import localCache from '@/utils/localCache'
import { computed, markRaw, toRaw } from 'vue'
import { Commit } from 'vuex'
import * as login from './actionsTypes'
import { IAdmin, IUserInfo } from './types'

interface ICX{
  commit:Commit
  state:IAdmin
}

const state = {
  userInfo: ''
}
const mutations = {
  [login.LOGIN] (state:IAdmin, data:IUserInfo) {
    state.userInfo = data
    const userInfo = toRaw(state.userInfo)
    localCache.setCache('USERINFO', userInfo)
  }
}
const actions = {
  async [login.LOGIN] ({ commit }:ICX, loginForm:IloginForm) {
    const result = await reqLogin(loginForm)
    commit(login.LOGIN, result.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
